package com.byapp.fast.modules.contract.controller;

import com.byapp.fast.amqp.enums.AmqpEnum;
import com.byapp.fast.common.exception.BYException;
import com.byapp.fast.common.utils.JsonUtils;
import com.byapp.fast.common.utils.ResultUtils;
import com.byapp.fast.common.validator.ValidatorUtils;
import com.byapp.fast.common.vo.ResultVO;
import com.byapp.fast.modules.contract.dto.CallBackEthDTO;
import com.byapp.fast.modules.contract.dto.CallBackPayDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api(value = "区块回调接口", tags = {"区块回调接口"})
public class ContractController {

    @Autowired
    private RabbitTemplate template;

    @Autowired
    private DirectExchange direct;

    @PostMapping("/eth/callBack")
    @ApiOperation(value = "区块回调", notes = "区块回调")
    @ApiImplicitParam(name = "callBackEthDTO", value = "区块回调实体", required = true, dataType = "CallBackEthDTO")
    public String callBackEth(@RequestBody CallBackEthDTO callBackEthDTO) {
        Integer cType = callBackEthDTO.getCType();
        System.out.println(cType);
       /* // 根据传入的消息类型找到对应的枚举类
        AmqpEnum.CTypeEnum codeEnum = EnumUtils.getByCode(cType, AmqpEnum.CTypeEnum.class);
        if (codeEnum == null) {
            log.error("【区块回调消息处理】消息类型不存在，参数：消息类型={}，原因={}", cType, ResultEnum.ILLEGAL_PARAM.getMsg());
            return ResultUtils.error().getMsg();
        }

        String key = codeEnum.getQueueName();
        log.info("【区块-{}消息】队列={},消息体={}", codeEnum.getMsg(), codeEnum.getQueueName(), JsonUtils.toJson(callBackEthDTO));*/
        // 使用exchange+routingKey的方式发送消息到队列中
        String key = AmqpEnum.MessageTypeEnum.CALLBACK_ETH.getQueueName();
        ResultVO result = (ResultVO) template.convertSendAndReceive(direct.getName(), key, callBackEthDTO);
        return result.getMsg();
    }

    @PostMapping("/pay/callBack")
    @ApiOperation(value = "支付回调", notes = "支付回调")
    @ApiImplicitParam(name = "callBackPayDTO", value = "支付回调实体", required = true, dataType = "CallBackPayDTO")
    public String callBackPay(@RequestBody CallBackPayDTO callBackPayDTO) {
        try {
            ValidatorUtils.validateEntity(callBackPayDTO);
        } catch (BYException e) {
            return ResultUtils.error().getMsg();
        }
        if (!StringUtils.equalsIgnoreCase(callBackPayDTO.getReturnCode(), "SUCCESS")) {
            return ResultUtils.error().getMsg();
        }
        log.info("【发送支付回调消息】callBackPayDTO={}", JsonUtils.toJson(callBackPayDTO));
        String key = AmqpEnum.MessageTypeEnum.CALLBACK_PAY.getQueueName();
        ResultVO result = (ResultVO) template.convertSendAndReceive(direct.getName(), key, callBackPayDTO);
        return result.getMsg();
    }


}
